﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using LHS.Core.TableInfo;
using MS.Core.Base.Transaction;
using System.Collections.Specialized;
using MS.Core.Constants.Objects;

namespace LHS.Win
{
    public partial class frmUsedMedicine : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmUsedMedicine()
        {
            this.InitializeComponent();
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            string str = string.Empty;
            if (this.chkAll.Checked)
            {
                this.frmUsedMedicine_Load(sender, e);
            }
            else if (this.tbKeySearch.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên thuốc cần tìm", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                ArrayList arrColumn = new ArrayList();
                arrColumn.Add(InfoMedicineStock.MIMID);
                arrColumn.Add(InfoMedicineStock.MIMID + " as ID");
                arrColumn.Add(InfoMedicineStock.MESMEDICINENAME);
                arrColumn.Add(InfoMedicineStock.MESQUANTITYINSTOCK);
                arrColumn.Add(InfoMedicineImport.MIMPRODUCTIONPERUNIT);
                arrColumn.Add(InfoMedicineStock.MESISN);

                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoMedicineStock.MESMEDICINENAME, this.tbKeySearch.Text.Trim(), ExpressionObject.eSelectOperator.LikeAll));
                DataTable dt = objController.GetData("vw_MedicineStock", arrColumn, param);

                if (dt == null && dt.Rows.Count == 0)
                {
                    MessageBox.Show("Không tìm thấy tên thuốc : " + this.tbKeySearch.Text.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    DataRow dr;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = dt.Rows[i];
                        dr[InfoMedicineStock.MESISN] = i + 1;
                        switch (dr[InfoMedicineImport.MIMPRODUCTIONPERUNIT].ToString())
                        {
                            case "0":
                                dr[InfoMedicineStock.MIMID] = "viên";
                                break;
                            case "1":
                                dr[InfoMedicineStock.MIMID] = "ống";
                                break;
                            case "2":
                                dr[InfoMedicineStock.MIMID] = "chai";
                                break;
                            case "3":
                                dr[InfoMedicineStock.MIMID] = "gói";
                                break;
                            case "4":
                                dr[InfoMedicineStock.MIMID] = "tuýp";
                                break;
                        }
                    }
                    bindingSource1.DataSource = dt;
                    this.dataGrid1.DataSource = bindingSource1;
                    this.dataGrid1.TableStyles.Clear();
                    this.loadDataGrid();
                }
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                if (MessageBox.Show("Bạn có muốn hủy tên thuốc này không ?", "Xóa Thuốc", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.dataGrid1.Select(this.dataGrid1.CurrentRowIndex);
                    string str = dataGrid1[this.dataGrid1.CurrentRowIndex, 4].ToString();

                    TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                    try
                    {
                        trans.BeginTransaction();
                        ListDictionary param = new ListDictionary();
                        param.Add(InfoMedicineStock.MIMID, str);
                        objController.Exec_StoreProc("sp_Medicine_Delete", param);
                        trans.CommitTransaction();
                        MessageBox.Show("Đã xóa tên thuốc trong kho", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    catch (Exception ex)
                    {
                        trans.RollbackTransaction();
                        Class.ShowException(ex);
                    }
                }
                this.frmUsedMedicine_Load(sender, e);
            }
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkAll.Checked)
            {
                this.tbKeySearch.Text = "";
            }
        }

        private void frmUsedMedicine_Load(object sender, EventArgs e)
        {
            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoMedicineStock.MIMID);
            arrColumn.Add(InfoMedicineStock.MIMID + " as ID");
            arrColumn.Add(InfoMedicineStock.MESMEDICINENAME);
            arrColumn.Add(InfoMedicineStock.MESQUANTITYINSTOCK);
            arrColumn.Add(InfoMedicineImport.MIMPRODUCTIONPERUNIT);
            arrColumn.Add(InfoMedicineStock.MESISN);
            DataTable dt = objController.GetData("vw_MedicineStock", arrColumn, null);

            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow dr;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dr = dt.Rows[i];
                    dr[InfoMedicineStock.MESISN] = i + 1;
                    switch (dr[InfoMedicineImport.MIMPRODUCTIONPERUNIT].ToString())
                    {
                        case "0":
                            dr[InfoMedicineStock.MIMID] = "viên";
                            break;
                        case "1":
                            dr[InfoMedicineStock.MIMID] = "ống";
                            break;
                        case "2":
                            dr[InfoMedicineStock.MIMID] = "chai";
                            break;
                        case "3":
                            dr[InfoMedicineStock.MIMID] = "gói";
                            break;
                        case "4":
                            dr[InfoMedicineStock.MIMID] = "tuýp";
                            break;
                    }
                }

                bindingSource1.DataSource = dt;
                this.dataGrid1.DataSource = bindingSource1;

                this.dataGrid1.TableStyles.Clear();
                this.loadDataGrid();
            }
        }

        public void loadDataGrid()
        {
            DataGridTableStyle table = new DataGridTableStyle();
            table.MappingName = "vw_MedicineStock";
            table.AlternatingBackColor = Color.AliceBlue;
            table.HeaderForeColor = Color.Maroon;
            table.GridLineColor = Color.CornflowerBlue;

            DataGridTextBoxColumn column = new DataGridTextBoxColumn();//0
            column.MappingName = InfoMedicineStock.MESISN;
            column.HeaderText = " STT";
            column.Width = 50;
            column.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column);

            DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();//1
            column2.MappingName = InfoMedicineStock.MESMEDICINENAME;
            column2.HeaderText = "           Tên Thuốc";
            column2.Width = 220;
            column2.NullText = string.Empty;
            column2.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column2);

            DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();//2
            column3.MappingName = InfoMedicineStock.MESQUANTITYINSTOCK;
            column3.HeaderText = "      Số Lượng Tồn";
            column3.Width = 150;
            column3.NullText = string.Empty;
            column3.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column3);

            DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();//3
            column4.MappingName = InfoMedicineStock.MIMID;
            column4.HeaderText = "     Đơn Vị Tính";
            column4.Width = 0x85;
            column4.NullText = string.Empty;
            column4.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column4);

            DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();//4
            column5.MappingName = "ID";
            column5.HeaderText = "";
            column5.Width = 0;
            column5.NullText = string.Empty;
            column5.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column5);
            table.AllowSorting = false;
            this.dataGrid1.TableStyles.Add(table);
        }

    }
}